<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Uri_strategy
{
    private $_route_map = array();
    
    public function __construct(Route_register $register)
    {
        $this->_route_map = $register->get_route_map();
        $this->_init();
    }
    
    private function _init()
    {
        $route_map = $this->_route_map;
        $request_uri = '/'.ltrim($_SERVER['REQUEST_URI'], '/index.php/');
        $request_method = strtolower($_SERVER['REQUEST_METHOD']);
        $route_map[$request_method] = array_reverse($route_map[$request_method], TRUE);
        
        foreach ($route_map[$request_method] as $route_reg => $fun)
        {
            $res = preg_match($route_reg, $request_uri, $matches);
            
            if ($res === 1)
            {
                $matches = array_slice($matches, 1);
                
                if (is_callable($fun))
                {
                    echo call_user_func_array($fun, $matches);
                    exit;
                }
                elseif (preg_match('/^[\w\d]+@[\w\d]+$/', $fun))
                {
                    $tmp = explode('@', $fun);
                    //
                    $this->directory = '';
                    $this->class = $tmp[0];
                    $this->method = $tmp[1];
                    var_dump($tmp);
                }
                
                break;
            }
            else
            {
                show_404();
            }
        }
    }
}